//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.aidong.dao.wills;

import com.aidong.dao.wills.BaseDao;
import com.aidong.model.wills.DicShopAreaCode;
import com.aidong.model.wills.DicShopCode;
import java.util.Iterator;
import java.util.List;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.springframework.stereotype.Repository;

@Repository
public class DicShopAreaCodeDao extends BaseDao<DicShopAreaCode, String> {
	public DicShopAreaCodeDao() {
	}

	public boolean chkAreaShop(String areaCode, String shopCode) {
		String sql = "select count(*) from DicShopAreaList where areaCode=:p1 and shopCode=:p2";
		return ((Integer)this.getBySql(Integer.class, sql, new Object[]{areaCode, shopCode})).intValue() > 0;
	}

	public List<String> getShopCodeByAreaCode(String areaCode) {
		String sql = "select shopCode from DicShopAreaList where areaCode=:p1";
		return this.queryListBySql(String.class, sql, new Object[]{areaCode});
	}

	public List<DicShopCode> getAreaShopList(String areaCode, boolean ball) {
		String sql;
		if(ball) {
			sql = "select s.code, s.chName, (case when a.shopCode is null then 0 else 1 end) status from DicShopCode s\nleft join DicShopAreaList a on s.code=a.shopCode and areaCode = :p1\nwhere status>0 order by s.code";
		} else {
			sql = "select s.code, s.chName, status from DicShopCode s\ninner join DicShopAreaList a on s.code=a.shopCode\nwhere areaCode = :p1 and status>0 order by s.code";
		}

		return this.queryListBySql(DicShopCode.class, sql, new Object[]{areaCode});
	}

	public void updateAreaShop(String areaCode, List<String> shopList) {
		Session session = this.getSession();
		SQLQuery query = session.createSQLQuery("delete from DicShopAreaList where AreaCode = :p1");
		query.setString("p1", areaCode);
		query.executeUpdate();
		query = session.createSQLQuery("insert into DicShopAreaList (AreaCode, ShopCode) values (:p1, :p2)");
		query.setString("p1", areaCode);
		Iterator i$ = shopList.iterator();

		while(i$.hasNext()) {
			String shopCode = (String)i$.next();
			query.setString("p2", shopCode);
			query.executeUpdate();
		}

	}
}
